﻿using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Management;
using WindowService;

namespace CProcessMonitor
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            ManagementBaseObject inPar = null;
            ManagementClass mc = new ManagementClass("Win32_Service");
            foreach (ManagementObject mo in mc.GetInstances())
            {
                if (mo["Name"].ToString() == "CProcessMonitor")
                {
                    inPar = mo.GetMethodParameters("Change");
                    inPar["DesktopInteract"] = true;
                    mo.InvokeMethod("Change", inPar, null);
                }
            }      

            //设置允许服务与桌面交互
            //SetServiceTable("CProcessMonitor");
        }
        /// <summary>
        /// 设置允许服务与桌面交互 ,修改了注册表，要重启系统才能生效
        /// </summary>
        /// <param name="ServiceName">服务程序名称</param>
        private void SetServiceTable(string ServiceName)
        {
            RegistryHelper registryHelper = new RegistryHelper();
            registryHelper.SetRegistryData(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\" + ServiceName, "Type", "272");
        }
    }
}
